<route lang="json5" type="page">
{
layout: 'default',
style: {
navigationStyle: 'custom',
navigationBarTitleText: 'report_work',
},
}
</route>
<template>
  <PageLayout :navTitle="navTitle" backRouteName="index" routeMethod="pushTab">
    <view class="form-container">
      <wd-form ref="form" :model="myFormData">
        <wd-cell-group border>
          <view class="{ 'mt-14px': 0 == 0 }">
            <wd-input label-width="100px" v-model="myFormData['addName']" :label="get4Label('姓名')" name='addName'
              prop='addName' placeholder="请输入姓名" :rules="[
              ]" clearable />
          </view>
          <view class="{ 'mt-14px': 0 == 0 }">
            <wd-input label-width="100px" v-model="myFormData['amount']" :label="get4Label('数量')" name='amount'
              prop='amount' placeholder="数量" inputMode="numeric" :rules="[
                {
                  required: true,
                  pattern: /^\d+$/,
                  message: '必须输入正整数',
                  trigger: 'blur'
                }
              ]" clearable />
          </view>
        </wd-cell-group>
      </wd-form>
    </view>

    <view class="footer">
      <wd-button :disabled="loading" block :loading="loading" @click="handleSubmit">提交</wd-button>
    </view>

  </PageLayout>
</template>

<script lang="ts" setup>
import { onLoad } from '@dcloudio/uni-app'
import { http } from '@/utils/http'
import { useToast } from 'wot-design-uni'
import { useRouter } from '@/plugin/uni-mini-router'
import { ref, onMounted, computed, reactive } from 'vue'
import OnlineImage from '@/components/online/view/online-image.vue'
import OnlineFile from '@/components/online/view/online-file.vue'
import OnlineFileCustom from '@/components/online/view/online-file-custom.vue'
import OnlineSelect from '@/components/online/view/online-select.vue'
import OnlineTime from '@/components/online/view/online-time.vue'
import OnlineDate from '@/components/online/view/online-date.vue'
import OnlineRadio from '@/components/online/view/online-radio.vue'
import OnlineCheckbox from '@/components/online/view/online-checkbox.vue'
import OnlineMulti from '@/components/online/view/online-multi.vue'
import OnlinePopupLinkRecord from '@/components/online/view/online-popup-link-record.vue'
import OnlinePca from '@/components/online/view/online-pca.vue'
import SelectDept from '@/components/SelectDept/SelectDept.vue'
import SelectUser from '@/components/SelectUser/SelectUser.vue'
import { duplicateCheck } from "@/service/api";

const toast = useToast()
const router = useRouter()
const form = ref(null)
// 定义响应式数据
const myFormData = reactive({})
const loading = ref(false)
const navTitle = ref('新增')
const dataId = ref('')
const backRouteName = ref('index')

// 提交表单
const handleSubmit = async () => {
  let url = dataId.value ? '/reportwork/reportWork/edit' : '/reportwork/reportWork/add';
  form.value
    .validate()
    .then(({ valid, errors }) => {
      if (valid) {
        loading.value = true;
        http.post(url, myFormData).then((res) => {
          loading.value = false;
          if (res.success) {
            toast.success('保存成功');
            handleSuccess()
          } else {
            toast.error(res?.message || '表单保存失败！')
          }
        })
      }
    })
    .catch((error) => {
      console.log(error, 'error')
      loading.value = false;
    })
}
const handleSuccess = () => {
  uni.$emit('refreshList');
  router.back()
}

// 标题
const get4Label = computed(() => {
  return (label) => {
    return label && label.length > 4 ? label.substring(0, 4) : label;
  }
})

</script>
